package com.chiemy.cardview;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.chiemy.cardview.view.CardAdapter;
import com.chiemy.cardview.view.CardView;
import com.chiemy.cardview.view.CardView.OnCardClickListener;
import com.hs.ble.BluetoothLeService;
import com.hs.color.lamp.ColorCntrlFragment;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;



public class MainActivity extends FragmentActivity implements OnCardClickListener{
	List<String> list;
	private TestFragment frag;
	private OTAFragment  ota_frag;
	private ColorLampFragment  color_lamp_frag;
	private FMFragment  fm_frag;
	private FragmentTransaction frangmenttrans;
	private SPPOTAFragment  spp_ota_frag;
	private HashMap<String, String> which_tag = new HashMap<String , String>();
	
	public static String HSTOOLSDIR = "HSTools";
	public static String HSFLASHCONTENT = "FlashContent";
	public static String HS6620_flash_manager_file = "HS6620_flash_manager.dat";
	
	private String tag = "MainActivity"; 
	public BluetoothAdapter mBluetoothAdapter;
	
	public BluetoothLeService mBLE;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//先给Activity注册界面进度条功能
		setContentView(R.layout.activity_main);
		init_data();
		initUI();	
		init_hstools_flash_data();
		
		
		String tmp = getPackageName();
		tmp = tmp +"";

	}
	
	@Override
    protected void onDestroy() {
        super.onDestroy();
        
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if(ColorCntrlFragment.mChatService != null)
		{
			if(ColorCntrlFragment.mChatService.getState() == ColorCntrlFragment.mChatService.STATE_CONNECTED)
			{
				ColorCntrlFragment.mChatService.stop();
			}
		}
		return super.onKeyDown(keyCode, event);

	}
	
	private void initUI() {
		CardView cardView = (CardView) findViewById(R.id.cardView1);
		cardView.setOnCardClickListener(this);
		cardView.setItemSpace(Utils.convertDpToPixelInt(this, 20));
		
		MyCardAdapter adapter = new MyCardAdapter(this);
		adapter.addAll(initData());
		cardView.setAdapter(adapter);
		
		FragmentManager manager = getSupportFragmentManager();
		frag = new TestFragment();
		ota_frag = new OTAFragment();
		color_lamp_frag = new ColorLampFragment();
		fm_frag = new FMFragment();
		spp_ota_frag = new SPPOTAFragment();
		frangmenttrans = manager.beginTransaction(); 
	} 
	
	@Override
	public void onCardClick(final View view, final int position) {
		//Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
		Bundle bundle = new Bundle();
		bundle.putString("text", list.get(position%list.size()));
		if("钱包OTA".equals(list.get(position%list.size()))){
			if(!ota_frag.isAdded())
				getSupportFragmentManager().beginTransaction().add(R.id.contentView, ota_frag).commit();
			ota_frag.show(view, bundle);
		}
		else if("SPP_OTA".equals(list.get(position%list.size()))){
			if(!spp_ota_frag.isAdded())
				getSupportFragmentManager().beginTransaction().add(R.id.contentView, spp_ota_frag).commit();
			spp_ota_frag.show(view, bundle);
		}
		else if("ColorLamp".equals(list.get(position%list.size()))){
			if(!color_lamp_frag.isAdded())
				getSupportFragmentManager().beginTransaction().add(R.id.contentView, color_lamp_frag).commit();
			color_lamp_frag.show(view, bundle);
		}else if("FM Music".equals(list.get(position%list.size()))){
			if(!fm_frag.isAdded())
				getSupportFragmentManager().beginTransaction().add(R.id.contentView, fm_frag).commit();
			fm_frag.show(view, bundle);
		}
		else{
			if(!frag.isAdded())
				getSupportFragmentManager().beginTransaction().add(R.id.contentView, frag).commit();
			frag.show(view, bundle);
		}
	}
	
	
	
	private List<String> initData() {
		list = new ArrayList<String>();
		list.add("钱包OTA");
		list.add("SPP_OTA");
		list.add("ColorLamp");
		if(Utils.UI_ONLY_SURPPORT_LED == false)
			list.add("FM Music");
//		list.add("e");
//		list.add("f");
//		list.add("g");
		return list;
	}

	public class MyCardAdapter extends CardAdapter<String>{

		public MyCardAdapter(Context context) {
			super(context);
		}
		
		@Override
		public int getCount() {
			return Integer.MAX_VALUE;
		}
		
		@Override
		protected View getCardView(int position,
				View convertView, ViewGroup parent) {
			if(convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
				convertView = inflater.inflate(R.layout.item_layout, parent, false);
			}
			TextView tv = (TextView) convertView.findViewById(R.id.textView1);
			String text = getItem(position%list.size());
			tv.setText(text);
			if(which_tag.containsKey(text)){
				tv = (TextView) convertView.findViewById(R.id.textView2);
				tv.setText(which_tag.get(text));
			}
			return convertView;
		}
	}
	
	private void init_data(){
		
		which_tag.put("钱包OTA", "通过这个service，可以将准备好的application、configuration、patch 进行空中升级！");
		which_tag.put("SPP_OTA", "通过SPP对HS6600系列芯片进行空升级 ！");
		which_tag.put("ColorLamp", "蓝牙音响灯demo，通过spp协议控制led灯，通过avrcp控制音量，通过a2dp播放音乐！");
		which_tag.put("FM Music", "车载蓝牙，手机通过蓝牙传输音乐，车载FM接收蓝牙音乐");
	}
	
	
	private void init_hstools_flash_data(){
		
		File sd = Environment.getExternalStorageDirectory(); 
		String path = sd.getPath()+"/" + HSTOOLSDIR; 
		File file = new File(path); 
		if(!file.exists())
		{
			if(!file.mkdir()){
				Log.i(tag, "init failed");
				return; 
			}
		}
		path = path + "/" + HS6620_flash_manager_file;
		File hs6620_flash_manager_file = new File(path);		
		if(!hs6620_flash_manager_file.exists()) 
		{
				
			//Write Default Boot Information
			Properties prop = new Properties();
			prop.put("magic_number", "0x36363230");
			prop.put("boot_info_data", "0x31323334");
			prop.put("app_offset", "0x1000");
			prop.put("app_load_addr", "0x20000000");
			prop.put("app_exe_addr", "0x00000000");
			prop.put("profile_offset", "0x5000");
			//prop.put("cfg_offset", "0x8000");
			prop.put("cfg_len", "0x010000");
			//prop.put("patch_offset", "0x018000");
			//Write Flash Managerment
			prop.put("size", "0x200000");
			prop.put("align", "0x1000");
			prop.put("boot_size", "0x1000");
			prop.put("patch_size_reserve", "0x000000");//0x010000
			prop.put("cfg_default_size", "0x010000");

			Utils op = new Utils();
			op.saveConfig(this, path, prop);
			try {
				file.createNewFile();
					
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
	}
	
}
